﻿/*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*
 *~*  16-Oct-2009  www.Learning-Leisure.com
 *~*  Main Project : Facebook Membership Provider
 *~*  Project : PB.CustomRoleProvider    Namespace : PB.CustomRoleProvider
 *~*  File    : DBMapper.cs                 Class  : DBMapper
 *~*  Author : Pinal Bhatt 
 *~*            self@P-Bhatt.com, www.P-Bhatt.com
 *~*   Version History
 *~*            Create Date         :     16-Oct-2009       Pinal Bhatt
 *~*           Freeze Date(Ver 1.0) :     23-Oct-2009       Pinal Bhatt  
 *~*            Last Update Date    :     23-Oct-2009       Pinal Bhatt  
 *~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*/

using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using PB.FBMembership.DAL;

namespace PB.CustomRoleProvider
{
    internal class DBMapper
    {
        #region Constants

        private const string PROC_GetUsersInRole = "SP_GetUsersInRole";
        private const string PROC_GetRolesForUser = "SP_GetRolesForUser";
        private const string PROC_AddRolesForUser = "SP_AddRolesForUser";
        private const string PROC_RemoveRolesForUser = "RemoveRolesForUser";

        #endregion

        #region DB Access Methods

        internal static List<string> GetRolesForUser(long fbUserId)
        {
            List<string> returnList = new List<string>();
            if (fbUserId > 0)
            {
                Hashtable parameters = new Hashtable();
                parameters.Add("pFBUserId", fbUserId);
                using (IDataReader reader = DBFacade.ExecuteDataReader(PROC_GetRolesForUser, parameters))
                {
                    if (reader != null)
                    {
                        while (reader.Read())
                        {
                            short tempid = DataReaderHelper.GetData<Int16>(reader, "RoleId");
                            returnList.Add(((RoleEnum)tempid).ToString());                            
                        }
                    }
                }
            }
            return returnList;
        }

        internal static List<string> GetUsersInRole(RoleEnum roleEnum)
        {
            List<string> returnList = new List<string>();
          
            Hashtable parameters = new Hashtable();
            parameters.Add("@pRoleId", (int)roleEnum);
            using (IDataReader reader = DBFacade.ExecuteDataReader(PROC_GetUsersInRole, parameters))
            {
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        long tempid = DataReaderHelper.GetData<Int64>(reader, "FBUserId");
                        returnList.Add(tempid.ToString());
                    }
                }
            }
            
            return returnList;
        }



        internal static void RemoveRolesForUser(long fbUserId, string roles)
        {
            if (fbUserId > 0 && !string.IsNullOrEmpty(roles))
            {
                Hashtable parameters = new Hashtable();
                parameters.Add("pFBUserId", fbUserId);
                parameters.Add("pRoles", roles);
                DBFacade.ExecuteNonQuery(PROC_RemoveRolesForUser, parameters);
            }
        }

 
        internal static void AddRolesForUser(long fbUserId, string roles)
        {
            if (fbUserId > 0 && !string.IsNullOrEmpty(roles))
            {
                Hashtable parameters = new Hashtable();
                parameters.Add("pFBUserId", fbUserId);
                parameters.Add("pRoles", roles);
                DBFacade.ExecuteNonQuery(PROC_AddRolesForUser, parameters);
            }
        }

        #endregion
    }
}
